package com.boyiis.sell.controller;

import com.boyiis.sell.converter.OrderForm2OrderDTOConverter;
import com.boyiis.sell.enums.ResultEnum;
import com.boyiis.sell.exception.SellException;
import com.boyiis.sell.model.dto.OrderDTO;
import com.boyiis.sell.model.form.OrderForm;
import com.boyiis.sell.model.vo.Result;
import com.boyiis.sell.service.BuyerService;
import com.boyiis.sell.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/seller/order")
@Slf4j
public class SellerOrderController {

    @Autowired
    private OrderService orderService;

    /**
     *订单列表
     * @param page
     * @param size
     * @return
     */
   @GetMapping("/list")
    public ModelAndView list(@RequestParam(value = "page", defaultValue = "1") Integer page, @RequestParam(value = "size", defaultValue = "10") Integer size){
       PageRequest request = new PageRequest(page-1, size);
       Page<OrderDTO> orderDTOPage =  orderService.findList(request);

       Map<String, Object> map = new HashMap<String, Object>();
       map.put("orderDTOPage", orderDTOPage);
       map.put("currentPage", page);
       map.put("size", size);
       return  new ModelAndView("order/list", map);

   }

    /**
     * 卖家取消订单
     * @param orderId
     * @return
     */
    @GetMapping("/cancel")
    public ModelAndView cancel(@RequestParam(value = "orderId") String orderId){
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("url", "/sell/seller/order/list");
        try{
            OrderDTO orderDTO = orderService.findOne(orderId);
            orderService.cancel(orderDTO);
        }catch (Exception e){
            log.error("【卖家端取消订单】 {}", e.getMessage());
            map.put("msg", e.getMessage());

            return new ModelAndView("common/error", map);
        }
        map.put("msg", ResultEnum.SUCCESS.getMsg());
        return  new ModelAndView("common/success", map);

    }

    @GetMapping("/detail")
    public ModelAndView detail(@RequestParam(value = "orderId") String orderId){
        Map<String, Object> map = new HashMap<String, Object>();

        try{
            OrderDTO orderDTO = orderService.findOne(orderId);
            map.put("orderDTO", orderDTO);
        }catch (Exception e){
            log.error("【卖家端查询订单详情】 {}", e.getMessage());
            map.put("msg", e.getMessage());
            map.put("url", "/sell/seller/order/list");
            return new ModelAndView("common/error", map);
        }

        return  new ModelAndView("order/detail", map);
    }

    @GetMapping("/finish")
    public ModelAndView finish(@RequestParam(value = "orderId") String orderId){
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("url", "/sell/seller/order/list");
        try{
            OrderDTO orderDTO = orderService.findOne(orderId);
            orderService.finish(orderDTO);
        }catch (Exception e){
            log.error("【卖家端查询订单详情】 {}", e.getMessage());
            map.put("msg", e.getMessage());
            return new ModelAndView("common/error", map);
        }
        map.put("msg", ResultEnum.ORDER_FINISH_ERROR.getMsg());
        return  new ModelAndView("common/success", map);
    }
}
